package org.wishwork.api.product.shop.upload;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.wishwork.api.Api;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 店铺商品资料上传
 * @author: WangYX
 * @create: 2022-04-11 15:15
 * @Version: 1.0.0
 **/
public class ShopProductUploadApi extends Api<ShopProductUploadResponse, List<ShopProductUploadRequest>> {

    private Context context;

    public ShopProductUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(List<ShopProductUploadRequest> shopProductUploadRequests) throws Exception {

        List<Map<String, Object>> data = new ArrayList<>();
        if (shopProductUploadRequests != null && shopProductUploadRequests.size() > 0) {
            shopProductUploadRequests.forEach(o -> {
                Map<String, Object> map = new HashMap<>();
                map.put("sku_id", o.getSkuId());
                if (StrUtil.isNotEmpty(o.getIId())) {
                    map.put("i_id", o.getIId());
                }
                if (StrUtil.isNotEmpty(o.getSkuCode())) {
                    map.put("sku_code", o.getSkuCode());
                }

                map.put("shop_i_id", o.getShopIId());
                map.put("shop_sku_id", o.getShopSkuId());

                if (StrUtil.isNotEmpty(o.getOriginalSkuId())) {
                    map.put("original_sku_id", o.getOriginalSkuId());
                }
                if (StrUtil.isNotEmpty(o.getName())) {
                    map.put("name", o.getName());
                }
                if (StrUtil.isNotEmpty(o.getShopPropertiesValue())) {
                    map.put("shop_properties_value", o.getShopPropertiesValue());
                }
                if (StrUtil.isNotEmpty(o.getSkuSign())) {
                    map.put("sku_sign", o.getSkuSign());
                }
                map.put("shop_id", o.getShopIId());
                data.add(map);
            });
        }

        String url = "/open/jushuitan/skumap/upload";
        Map<String, Object> params = new HashMap<>();
        params.put("items", data);
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected ShopProductUploadResponse process(JSONObject data, List<ShopProductUploadRequest> shopProductUploadRequests) throws Exception {
        return null;
    }
}
